//
//  UIBarButtonItem+Extension.m
//  WeiBo
//
//  Created by youmy on 15/8/18.
//  Copyright (c) 2015年 youmy. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"

#define kRightBtnFont 14

@implementation UIBarButtonItem (Extension)

+ (instancetype)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image hightImage:(NSString *)hightImage{
    // 设置右边的更多按钮
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    // 设置图片
    [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:hightImage] forState:UIControlStateHighlighted];
    
    // 设置尺寸
    btn.size = btn.currentBackgroundImage.size;
    
    btn.x = -20;
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}


+ (instancetype)itemWithTarget:(id)target action:(SEL)action title:(NSString *)title titleColor:(UIColor *)color{
    // 设置右边的更多按钮
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    // 设置按钮属性
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:kRightBtnFont];
    
    // 设置尺寸
    btn.width = [title sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:kRightBtnFont]}].width;
    
    
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (instancetype)item{
    // 设置右边的更多按钮
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+(UIBarButtonItem *)itemWithTarget:(id)target Action:(SEL)action nomalImage:(UIImage *)image ImageSelected:(UIImage *)imageSelected WithScale:(CGFloat)scale
{
    UIButton *btn = [[UIButton alloc] init];
    btn.bounds = CGRectMake(0, 0,image.size.width*scale,image.size.height*scale);
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:imageSelected forState:UIControlStateHighlighted];
    //设置按钮监听事件
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return  [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+(UIBarButtonItem *)itemWithTarget:(id)target Action:(SEL)action Title:(NSString *)title Font:(UIFont *)font
{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    btn.titleLabel.font = font;
    [btn setTitle:title forState:UIControlStateNormal];
    //设置按钮监听事件
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return  [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+(UIBarButtonItem *)itemWithTarget:(id)target Action:(SEL)action Title:(NSString *)title Font:(UIFont *)font Color:(UIColor *)color
{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    //    btn.backgroundColor = [UIColor redColor];
    btn.titleLabel.font = font;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    //设置按钮监听事件
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return  [[UIBarButtonItem alloc] initWithCustomView:btn];
}

@end


//private let kRightBtnFont = 14
//
//extension UIBarButtonItem {
//    
//    
//    /// 返回一个UIBarButtonItem
//    ///
//    /// - Parameters:
//    ///   - target: 目标
//    ///   - action: 监听方法
//    ///   - image: 图片名称
//    ///   - hightImage: 图片名称
//    /// - Returns: UIBarButtonItem
//    class func itemWithTargetActionImageHeightImage(target:AnyObject,action:Selector,image:String,hightImage:String) ->UIBarButtonItem{
//        // 设置右边的更多按钮
//        let btn = UIButton.init(type: .custom)
//        btn.addTarget(target, action: action, for: .touchUpInside)
//        // 设置图片
//        btn.setBackgroundImage(UIImage.init(named: image), for: .normal)
//        btn.setBackgroundImage(UIImage.init(named: hightImage), for: .highlighted)
//        
//        // 设置尺寸
//        btn.size = (btn.currentBackgroundImage?.size)!;
//        
//        btn.x = -20;
//        return UIBarButtonItem.init(customView: btn)
//    }
//    
//    class func itemWithTargetActionTitleTitleColor(target:Any,action:Selector,title:String,titleColor:UIColor) -> UIBarButtonItem {
//        // 设置右边的更多按钮
//        let btn = UIButton.init(type: .custom)
//        btn.addTarget(target, action: action, for: .touchUpInside)
//        
//        // 设置按钮属性
//        btn.setTitle(title, for: .normal)
//        btn.setTitleColor(titleColor, for: .normal)
//        
//        btn.titleLabel?.font = UIFont.systemFont(ofSize: CGFloat(kRightBtnFont))
//        
//        // 设置尺寸
//        let dict = [NSFontAttributeName:UIFont.systemFont(ofSize: CGFloat(kRightBtnFont))]
//        let size = (title as String).size(attributes: dict) //Even stranger errors go here
//        
//        btn.width = size.width;
//        
//        return UIBarButtonItem.init(customView: btn)
//    }
//    
//    class func item() -> UIBarButtonItem {
//        // 设置右边的更多按钮
//        let btn = UIButton.init(type: .custom)
//        return UIBarButtonItem.init(customView: btn)
//    }
//    class func itemWithTargetActionNomalImageImageSelectedScale(target:AnyObject,action:Selector,nomalImage:UIImage,imageSelected:UIImage,scale:CGFloat) -> UIBarButtonItem {
//        
//        let btn = UIButton.init()
//        btn.bounds = CGRect(x:0,y:0,width:nomalImage.size.width * scale,height:nomalImage.size.height * scale)
//        btn.setImage(nomalImage, for: .normal)
//        btn.setImage(imageSelected, for: .highlighted)
//        
//        //设置按钮监听事件
//        btn.addTarget(target, action: action, for: .touchUpInside)
//        return UIBarButtonItem.init(customView: btn)
//    }
//    
//    class func itemWithTargetActionTitleFont(target:AnyObject,action:Selector,title:String,font:UIFont) -> UIBarButtonItem {
//        
//        let btn = UIButton.init(frame: CGRect(x:0,y:0,width:80,height:40))
//        btn.titleLabel?.font = font;
//        btn.setTitle(title, for: .normal)
//        //设置按钮监听事件
//        btn.addTarget(target, action: action, for: .touchUpInside)
//        return UIBarButtonItem.init(customView: btn)
//    }
//    class func itemWithTargetActionTitleFontColor(target:AnyObject,action:Selector,title:String,font:UIFont,color:UIColor) -> UIBarButtonItem {
//        let btn = UIButton.init(frame: CGRect(x:0,y:0,width:80,height:40))
//        
//        btn.titleLabel?.font = font;
//        btn.setTitle(title, for: .normal)
//        btn.setTitleColor(color, for: .normal)
//        //设置按钮监听事件
//        btn.addTarget(target, action: action, for: .touchUpInside)
//        
//        return UIBarButtonItem.init(customView: btn)
//    }
//    
//    /*
//     创建导航栏左右侧按钮
//     */
//    static func createBBIWithFrameTitleImageTargetSEL(frame:CGRect,title:String,image:String,target:AnyObject,sel:Selector) -> UIButton {
//        let button = UIButton.init(type: .custom)
//        button.frame = frame
//        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
//        button.setTitle(title, for: .normal)
//        button.setTitleColor(UIColor.black, for: .normal)
//        button.setBackgroundImage(UIImage.init(named: image), for: .normal)
//        button.addTarget(target, action: sel, for: .touchUpInside)
//        return button
//    }
//    static func createButtonWithFrameTitleImageTargetSel(frame:CGRect,title:String,image:UIImage,target:AnyObject,sel:Selector) -> UIButton {
//        let button = UIButton.init(type: .custom)
//        button.setImage(image, for: .normal)
//        button.frame = frame
//        var newFrame = frame
//        newFrame.origin.y = frame.height / 2
//        newFrame.size.height = frame.height / 2
//        newFrame.origin.x = 0
//        
//        let label = UILabel.init(frame: newFrame)
//        label.text = title
//        button.addSubview(label)
//        
//        label.font = UIFont.systemFont(ofSize: 12)
//        label.backgroundColor = UIColor.clear
//        
//        button.addTarget(target, action: sel, for: .touchUpInside)
//        return button
//    }
//}
